from Config.myConstant import *
from Config.myConfig import *
from DataPrepare.tickFactors.factorBase import factorBase
from DataAccess.TickDataProcess import TickDataProcess
import pandas as pd
import numpy as np
########################################################################
class targetFactor2(factorBase):
    """描述训练目标的函数"""
    #----------------------------------------------------------------------
    def __init__(self):
        #super(buySellVolumeRatio,self).__init__()
        super().__init__()
        self.factor='targetFactor'
        pass
    #----------------------------------------------------------------------
    def getFactorFromLocalFile(self,code,date):
        mydata=super().getFromLocalFile(code,date,'targetFactor')
        return mydata
        pass
    #----------------------------------------------------------------------
    def updateFactor(self,code,date,data=pd.DataFrame()):
        exists=super().checkLocalFile(code,date,self.factor)
        if exists==True:
            logger.info(f'No need to compute! {self.factor} of {code} in {date} exists!')
            pass
        if data.shape[0]==0:
             #data=TickDataProcess().getDataByDateFromLocalFile(code,date)
             data=TickDataProcess().getTickShotDataFromInfluxdbServer(code,date)
        result=self.computerFactor(code,date,data)
        super().updateFactor(code,date,self.factor,result)
    #----------------------------------------------------------------------
    def computerFactor(self,code,date,mydata):
        result=pd.DataFrame()
        if mydata.shape[0]!=0:
            #index对齐即可
            result=mydata[['midPrice','tick']].copy()
            result['midPrice'].fillna(method='ffill',inplace=True)
            mydata['sellPrice']=mydata['B1']
            mydata['buyPrice']=mydata['S1']
            select=(mydata['B1']==0) | (mydata['S1']==0)
            mydata.loc[select,'sellPrice']=mydata['midPrice'][select]
            mydata.loc[select,'buyPrice']=mydata['midPrice'][select]
            mydata['buyPriceMeanPrevious1m']=mydata['buyPrice'].rolling(20,min_periods=1).mean()
            mydata['sellPriceMeanPrevious1m']=mydata['sellPrice'].rolling(20,min_periods=1).mean()
            mydata['buyPriceStdPrevious5m']=mydata['buyPrice'].rolling(100,min_periods=1).std()
            mydata['sellPriceStdPrevious5m']=mydata['sellPrice'].rolling(100,min_periods=1).std()
            #费后打对手价格的夏普率
            result['buySharpeNext5m']=(mydata['sellPriceMeanPrevious1m'].shift(-120)/mydata['buyPriceMeanPrevious1m']-1-0.0012)/(mydata['sellPriceStdPrevious5m'].shift(-100)+0.0001)
            result['sellSharpeNext5m']=(mydata['buyPriceMeanPrevious1m'].shift(-120)/mydata['sellPriceMeanPrevious1m']-1+0.0012)/(mydata['buyPriceStdPrevious5m'].shift(-100)+0.0001)
            #mid incr
            #
            # ease mean
            result['midIncreaseMeanNext1m'] = mydata['midPrice'].rolling(20).mean().shift(-40) / mydata['midPrice'] - 1
            result['midIncreaseMeanNext2m'] = mydata['midPrice'].rolling(20).mean().shift(-60) / mydata['midPrice'] - 1
            result['midIncreaseMeanNext5m'] = mydata['midPrice'].rolling(20).mean().shift(-120) / mydata['midPrice'] - 1
            #------------------------------------------------------------------
            #剔除14点57分之后，集合竞价的数据
            result=result[result['tick']<'145700000']
            mycolumns=list(set(result.columns).difference(set(mydata.columns)))
            mycolumns.sort()
            result=result[mycolumns]
            #super().checkDataNan(code,date,self.factor,result)
            pass
        else:
            logger.error(f'There no data of {code} in {date} to computer factor!') 
        return result
########################################################################

